package com.kucowka.db.dao;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateAccessor;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.kucowka.db.entities.User;

@Repository
public class UserDaoImpl implements UserDao {

	private HibernateTemplate hibernateTemplate;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
		hibernateTemplate.setFlushMode(HibernateAccessor.FLUSH_AUTO);
	}

	public List<User> getUsers() {
		List<User> list = (List<User>)hibernateTemplate.find("from " + User.class.getName());
		
		/*User user2 = (User) hibernateTemplate.get(User.class.getName(), 1L);
		user2.setName("jadam");
		
		User user3 = (User) hibernateTemplate.get(User.class.getName(), 1L);
		user3.setName("jozko2");
		
		hibernateTemplate.saveOrUpdate(user2);
		hibernateTemplate.saveOrUpdate(user3);
		boolean a = true;
		
		if(a)
			throw new RuntimeException();*/
		
		return list;
	}
	
	public void createUser(User user) {
		
		System.out.println("create user " + user);
		
		hibernateTemplate.save(user);
	}

	public void removeUser(int id) {
		User user = new User();
		user.setId(id);
		
		hibernateTemplate.delete(user);
		
		//throw new RuntimeException();
	}
}
